// coaches-detail.js
var app = getApp()
var httpclient = require('../../utils/httpclient.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    modalContent: "",
    modalHidden: true,
    coachId : '',
    coachName :　'',
    alreadyPT : false,
    coachs : []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
    const coachId = options.id;
    const coachName = options.coachName;
    console.log('coachId:' + coachId);
    const that = this;
    this.setData({
      coachId: coachId,
      coachName : coachName
    });
    wx.request({
      url: app.globalData.url + '/cms/news/detail',
      data: {
        id: coachId
      },
      success: function (res) {
     
        const coach = res.data.data;
        that.setData({
          coach
        })
      }
    });

    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  initendTimeData: function (startTime) {

  },
  appointtap : function(){
    this.setData({
      modalContent: '',
      modalHidden: false
    });
  },
  modalHide: function () {
    const that = this;
    //参与的团购
    let tuns = [];
    wx.getStorage({
      key: 'tuns',
      success: function (res) {
        tuns = res.data;
        
        if (tuns.length > 0) {
          for (var i =0; i < tuns.length; i++) {
            console.log(tuns[i] == that.data.coachName)
            if (tuns[i] == that.data.coachName) {

              that.setData({ alreadyPT: true });
              break;
            } else {
              that.setData({ alreadyPT: false });
            }
          }
        }
        that.coommitData(tuns);
      },
    });
    

  },
  coommitData: function (tuns){
    const that = this;
    //false表示之前没有拼过
    if (that.data.alreadyPT) {
      wx.showModal({
        title: '拼团通知',
        content: '您已经成功拼过团',
      })
      that.setData({
        modalHidden: true,
        modalContent: ''
      })

    } else {

      if (app.globalData.nickname == null) {
        wx.redirectTo({
          url: '../start/start',
        })
      } else {

        tuns.push(that.data.coachName);
        wx.setStorageSync('tuns', tuns);

        that.setData({ alreadyPT: true });
        wx.showModal({
          title: '拼团通知',
          content: '拼团成功，谢谢您的参与，请尽快支付，谢谢',
        })
        that.setData({
          modalHidden: true,
          modalContent: ''
        })

        console.log(that.data.alreadyPT)
      }

    }
  },
  modalHideCancle : function(){
    this.setData({
      modalContent: '',
      modalHidden: true
    });
  }
})